/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2021-10-12
 * Time: 22:40
 */
class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) {
          this.val = val;
      }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
         this.left = left;
          this.right = right;
      }
}

public class BinaryTree {
    public void tree2strChild(TreeNode t,StringBuilder sb){
        if(t==null){
            return;
        }
        sb.append(t.val);
        if(t.left==null){
            if(t.right==null){
                return;
            }else{
                sb.append("()");
            }
        }else{
            sb.append("(");
            tree2strChild(t.left,sb);
            sb.append(")");
        }
        if(t.right==null){
            return;
        }else{
            sb.append("(");
            tree2strChild(t.right,sb);
            sb.append(")");
        }

    }
    public String tree2str(TreeNode root) {
        if(root==null){
            return null;
        }
        StringBuilder sb=new StringBuilder();
        tree2strChild(root,sb);
        return sb.toString();

    }
}
